<template>
  <div>
    <AplidForm ref="formRef" v-bind="ExtendFormConfig" v-model="formModel" @submit="onSubmit"> </AplidForm>
  </div>
</template>

<script setup lang="ts">
import apiMod from '@/apiMod'
import { ExtendFormConfig } from '../../config'
import { useCommonStore } from '@/stores/common'

const emit = defineEmits(['refresh'])
const formModel = ref({})
const formRef = ref(null)

const onSubmit = (form, done) => {
  const { tempNowObj } = storeToRefs(useCommonStore())
  const ServiceOrgItems =
    form.ServiceOrgItems &&
    form.ServiceOrgItems.map((item) => {
      return {
        ...item,
        organizationName: tempNowObj.value.organizationName,
        creditCode: tempNowObj.value.creditCode,
        code: 3016,
      }
    })
  const params = {
    dpServiceOrgExtendPO: {
      ...form,
      creditCode: tempNowObj.value.creditCode,
      organizationName: tempNowObj.value.organizationName,
      ServiceOrgItems: undefined,
      code: 3015,
    },
    dpServiceOrgItemsPOS: ServiceOrgItems || [],
  }
  apiMod
    .view_dpServiceOrgExtend_gatherAll(params)
    .then((res) => {
      emit('refresh', res.data)
      done()
    })
    .catch((err) => {
      done()
    })
}

const onSave = () => {
  formRef.value!.onSubmit()
}
defineExpose({
  onSave,
})
</script>

<style scoped></style>
